const axios = require("axios");
const fs = require("fs");

let startTime = 0;

// 批量下载图片到指定文件夹中
module.exports = function (url, dir, fileName = Date.now()) {
  if (Date.now() - startTime < 300) {
    console.log("频率过高，请降低下载频率");
    return;
  }

  startTime = Date.now();
  try {
    axios
      .get(url, {
        // 设置返回的类型是二进制
        responseType: "arraybuffer",
      })
      .then((res) => {
        // 下载当前图片文件到文件夹中
        fs.writeFile(`${dir}/${fileName}.png`, res.data, "binary", (err) => {
          if (err) {
            console.log(`error`, err);
          } else {
            console.log(url, dir, fileName, "下载成功", `finish`);
          }
        });
      })
      .catch((err) => {
        console.log(err);
      });
  } catch (error) {
    console.log(error, "axios错误");
  }
};
